﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using Microsoft.WindowsAPICodePack.Dialogs;


namespace howto_treeview_directory
{
    public partial class Form1 : Form
    {
        const string filePath = "path.txt";
        public Form1()
        {
            InitializeComponent();
        }

        // Go up two directories and build a TreeView from there.
        private void Form1_Load(object sender, EventArgs e)
        {
            if (!File.Exists(filePath))
            {
                //MessageBox.Show(filePath + "  not exists!");
                FileStream fs = File.Create(filePath);//创建文件

                //写入默认路径--仓库例子
                string msg = "..\\..\\..\\example-reports";
                //将字符串转换为字节数组
                byte[] bytes = Encoding.UTF8.GetBytes(msg);
                //向文件中写入字节数组
                fs.Write(bytes, 0, bytes.Length);

                fs.Close();
            }

            string rootpath = File.ReadAllText(@"path.txt");
            Console.WriteLine(rootpath);

            //string dir = Path.Combine(rootpath, "..\\..");
            string dir = Path.Combine(rootpath, "");
            DirectoryInfo dir_info = new DirectoryInfo(dir);

            trvDirectory.LoadFromDirectory(dir_info.FullName, 0, 1);
            trvDirectory.ExpandAll();
            trvDirectory.SelectedNode = trvDirectory.Nodes[0];
        }

        private void Button1_MouseClick(object sender, MouseEventArgs e)
        {
            //MessageBox.Show(e.Button.ToString() + sender.ToString());
            /*folderBrowserDialog1.SelectedPath = txtDirectoryPath.Text;
            DialogResult drResult = folderBrowserDialog1.ShowDialog();
            if (drResult == System.Windows.Forms.DialogResult.OK)
                txtDirectoryPath.Text = folderBrowserDialog1.SelectedPath;*/

            //DialogResult dr = openFileDialog1.ShowDialog();
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.InitialDirectory = ".\\";
            dialog.IsFolderPicker = true;
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                MessageBox.Show("You selected: " + dialog.FileName);
                txtDirectoryPath.Text = dialog.FileName;
            }
        }

        private void Button_Filter_Click(object sender, EventArgs e)
        {

        }
    }
}
